﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FimetContoDeposito.DAL;
using GrayParrot.Core;

namespace FimetContoDeposito.BLL.Facades
{
    public class DettaglioScaricoBollaFacade : FacadeBase<DettaglioScaricoBollaFacade>
    {
        FCDContext m_ctx = null;

        public Repository<FCD_DettaglioScaricoBolla, Guid> Repo { get; internal set; }

        public DettaglioScaricoBollaFacade()
        {
        }

        public override void Initialize()
        {
            m_ctx = new FCDContext();
            m_ctx.Configuration.LazyLoadingEnabled = false;
            m_ctx.Configuration.ProxyCreationEnabled = false;

            Repo = this.GetRepository<FCD_DettaglioScaricoBolla, Guid>(m_ctx);
        }

        public override void Dispose()
        {
            if (m_ctx != null)
            {
                m_ctx.Dispose();

                Repo.Dispose();
            }
        }

        public ReturnValue<bool> Add(FCD_DettaglioScaricoBolla item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Add(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Edit(FCD_DettaglioScaricoBolla item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Edit(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(FCD_DettaglioScaricoBolla item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Delete(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<FCD_DettaglioScaricoBolla> Get(Guid id)
        {
            ReturnValue<FCD_DettaglioScaricoBolla> retValue = new ReturnValue<FCD_DettaglioScaricoBolla>();

            try
            {
                retValue.Value = this.Repo.Get(id);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<FCD_DettaglioScaricoBolla>> GetAll()
        {
            ReturnValue<IQueryable<FCD_DettaglioScaricoBolla>> retValue = new ReturnValue<IQueryable<FCD_DettaglioScaricoBolla>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll();
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        /// <summary>
        /// Verifica che l'elemento di un movimento di carico non sia usato in un movimento di scarico...
        /// </summary>
        /// <param name="idElemento">The identifier elemento.</param>
        /// <returns></returns>
        public ReturnValue<bool> CheckInScarico(Guid idElemento)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>(false);

            var objs = from x in m_ctx.FCD_DettaglioScaricoBollas
                       where x.IdBollaCarico == idElemento
                       select x;

            if (objs.Any())
            {
                retValue.HasValue = true;
                retValue.Value = true;
            }

            return retValue;
        }
    }
}
